package edu.joe.demo.service;

import conventer.IConverter;
import edu.joe.demo.bean.Msg;
import edu.joe.demo.component.DbDaoComponent;
import edu.joe.demo.conventer.MibDbModuleConverter;
import edu.joe.demo.conventer.MibDbNodeConverter;
import edu.joe.demo.entity.MibDbModule;
import edu.joe.demo.entity.MibDbNode;
import edu.joe.demo.mapper.MibModuleMapper;
import edu.joe.demo.mapper.MibNodeMapper;
import manager.IMibManager;
import manager.MibManager;
import model.MibModule;
import model.MibNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.mib.MibUtils;
import util.mib.tree.MibZtreeUtil;

import java.util.*;

@Service
public class MibService {
    @Autowired
    MibModuleMapper moduleMapper;
    @Autowired
    MibNodeMapper nodeMapper;
    @Autowired
    DbDaoComponent dbDaoComponent;

    public Msg getNodeTreeByMibs(Collection<String> mibs){
        MibDbNodeConverter converter = new MibDbNodeConverter();
        List<MibDbNode> nodes = new LinkedList<>();
        for (String mibName : mibs){
            nodes.addAll(nodeMapper.getByModuleName(mibName));
        }
        List<MibNode> beans = converter.toBeans(nodes);
        return Msg.getSuccessMsg(generateMibTree(beans));
    }

    public Msg getNodeTree(int[] ids){
        MibDbNodeConverter converter = new MibDbNodeConverter();
        List<MibDbNode> nodes = new LinkedList<>();
        Map<String, MibDbModule> nameModuleMap = new HashMap<>();
        for (int id : ids){
            MibDbModule module = moduleMapper.getOne(id);
            if (null != module && !nameModuleMap.containsKey(module.getModuleName())){
                nodes.addAll(nodeMapper.getByModuleId(module.getId()));
                nameModuleMap.put(module.getModuleName(), module);
            }
        }
        List<MibNode> beans = converter.toBeans(nodes);
        return Msg.getSuccessMsg(generateMibTree(beans));
    }

    private Object generateMibTree(List<MibNode> beans){
        // 转化成树状结构
        beans = MibUtils.GenerateMibTreesByName(beans);
        // 转化为Ztree格式
        Object tree =  MibZtreeUtil.TransferToDescriptionTree(beans);
        return tree;
    }

    @Transactional
    public Msg uploadMibFile(String txt){
        IMibManager manager = new MibManager(dbDaoComponent);
        MibModule module = null;
        try {
            module = manager.analysisMibText(txt);
            if (module == null && module.moduleName == null){
                throw new Exception("module模块为空。");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Msg.getErrorMsg("解析失败：" + e.getMessage());
        }

        if (moduleMapper.isModuleExist(module.moduleName) > 0){
            // 已存在则删除并重新导入
            moduleMapper.deleteByModuleName(module.moduleName);
        }
        IConverter conventer = new MibDbModuleConverter();
        MibDbModule dbModule = ((MibDbModuleConverter) conventer).toEntity(module);
        moduleMapper.save(dbModule);

        conventer = new MibDbNodeConverter();
        Collection<MibDbNode> mibDbNodeList = ((MibDbNodeConverter) conventer).toEntities(module.nodes.values());
        mibDbNodeList.forEach(mibDbNode -> mibDbNode.module = dbModule);
        nodeMapper.saveAll(mibDbNodeList);
        return Msg.getSuccessMsg();
    }

    public Msg getAllMibs(){
        return Msg.getSuccessMsg(moduleMapper.findAll());
    }

    @Transactional
    public Msg deleteMib(int id){
        try {
            moduleMapper.deleteById(id);
            return Msg.getSuccessMsg();
        }catch (Exception e){
            e.printStackTrace();
            return Msg.getErrorMsg(e);
        }
    }
}
